package com.ruoyi.demo.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.demo.mapper.StatisticalAnalysisDemoMapper;
import com.ruoyi.demo.service.JyDemoService;
import com.ruoyi.demo.service.StatisticalAnalysisDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.ServletContext;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wxx
 * @description
 * @date
 * @motto
 */
@Service
public class StatisticalAnalysisDemoServiceImpl implements StatisticalAnalysisDemoService {
    @Autowired
    JyDemoService jyService;
    @Autowired
    ServletContext servletContext;

    StatisticalAnalysisDemoMapper statisticalAnalysisDemoMapper;
    @Override
    public int InboundDataOftAlarm(Map map) {

        //Inbound data 入库数据
//        TAlarms tAlarm = JSON.parseObject(map.get("data").toString(),TAlarms.class);
        Map<String, Object> maps = JSON.parseObject(map.get("data").toString());

        HashMap<Object, Object> insertMap = new HashMap<>();
        /*
        事件类型 暂未用到
         */
        String eventType = map.get("event_type").toString();
        insertMap.put("eventType",eventType);
        /*
        事件id 暂未用到
         */
        String eventId = map.get("event_id").toString();
        insertMap.put("eventId",eventId);
        /*
        报警等级  5 4 3 2 1 分别对应 一级报警 二级报警 三级报警 四级报警 五级报警
         */
        String alarmLevel = maps.get("alarm_level").toString();
        insertMap.put("alarmLevel",alarmLevel);

        String alarmLevelName = maps.get("alarm_level_name").toString();
        insertMap.put("alarmLevelName",alarmLevelName);
        /*
        报警类型 alarm_type 对应 报警表 alarmType
         */
        String alarmType = maps.get("alarm_type").toString();
        insertMap.put("alarmType",alarmType);
        /*
        报警类型名称  alarm_type_name 对应 报警表 alarmTypeName
         */
        String alarmTypeName = maps.get("alarm_type_name").toString();
        insertMap.put("alarmTypeName",alarmTypeName);

        /*
        监控点id   device_id 对应 报警表 deviceId
         */
        String deviceId = maps.get("device_id").toString();
        insertMap.put("deviceId",deviceId);

        /*
        监控点名称   device_name 对应 报警表 deviceName
         */
        String deviceName = maps.get("device_name").toString();
        insertMap.put("deviceName",deviceName);

        String idStr = maps.get("id").toString();
        insertMap.put("idStr",idStr);
        /*
        监控点名称   device_name 对应 报警表 deviceName
         */
        String orgIndex = maps.get("org_index").toString();
        insertMap.put("orgIndex",orgIndex);

        String orgName = maps.get("org_name").toString();
        insertMap.put("orgName",orgName);

        String pictureUrl = maps.get("picture_url").toString();
        insertMap.put("pictureUrl",pictureUrl);

        Map<String, Object> bizJson = (Map<String, Object>) maps.get("biz_json");
        //设备名称 替换 orgName
        String puName = bizJson.get("pu_name").toString();
        insertMap.put("orgName",puName);


        String timeForStr = maps.get("alarm_time").toString();
        Long currentMilltimes = Long.valueOf(timeForStr);
        //报警时间获取
        String alarmTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentMilltimes);
        //获取报警时间

        insertMap.put("alarmTime",alarmTime);

        //报警图片转 相对路径转 http
        ArrayList<String> pictureUrlList = new ArrayList<>();
        pictureUrlList.add(pictureUrl);

        String s="";
        //当图片转化失败时 进行异常处理
        try {
            s = jyService.picAddrTrans(pictureUrlList);
        } catch (Exception e) {
            s = "";
        }

        Map<String, Object> Map = JSON.parseObject(s);
        if (Map==null||Map.get("data")==null){

        }else {
            List<String> urlList2 = (List<String>) Map.get("data");
            String newPictureUrl = urlList2.get(0);
            s=newPictureUrl;
        }
        insertMap.put("httpUrl",s);
        int i = statisticalAnalysisDemoMapper.insertInboundDataOftAlarm(insertMap);

        return i;
    }
    //1
}
